package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	//     "strconv"
	"path/filepath"
	"strings"
        "time"
)

func CopyFileToDirWithNewName(srcFile, destDir, newName string) error {
	// Open the source file
	src, err := os.Open(srcFile)
	if err != nil {
		return err
	}
	defer src.Close()

	// Ensure the destination directory exists
	err = os.MkdirAll(destDir, os.ModePerm)
	if err != nil {
		return err
	}

	// Create the destination file with the new name
	destPath := filepath.Join(destDir, newName)
	dest, err := os.Create(destPath)
	if err != nil {
		return err
	}
	defer dest.Close()

	// Copy the contents of the source file to the destination file
	_, err = io.Copy(dest, src)
	return err
}

func main() {
	f1, err1 := os.Open(os.Args[1])

	var fileDir string
	fileDir = "/data/new123" 

	if err1 == nil {
		buf := bufio.NewReader(f1)
		for {
			line, err := buf.ReadString('\n')
			if err != nil || io.EOF == nil {
				break
			}

			str1 := strings.Replace(line, "\n", "", -1)
			arrayLine := strings.Split(str1, "|")
			if len(arrayLine) == 4 && strings.Contains(arrayLine[1],"mp4") {

				fmt.Println(arrayLine[1], fileDir+arrayLine[0])
				err := CopyFileToDirWithNewName(arrayLine[1],fileDir , arrayLine[0])
				if err != nil {
                                        fmt.Println(time.Now(),err)
				}
				println("File copied successfully!")
                                
				fmt.Println("http://172.16.3.1/"+arrayLine[0]+","+arrayLine[2]+","+arrayLine[3])

			}

		}
	}

}
